﻿using System.Data.SqlClient;
using GeneralLibs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lists;
namespace ListForms
{
    public partial class FrmDanhMucPhuongThucThanhToanBanHang : ListForms.FrmBaseListForm
    {
        public FrmDanhMucPhuongThucThanhToanBanHang()
        {
            InitializeComponent();
            DanhMucPhuongThucThanhToanBanHang.Instance.Change += new DanhMucPhuongThucThanhToanBanHang.ChangeHandler(Instance_Change);
        }
        void Instance_Change()
        {
            LoadData();
        }
        public override void InitGridColumns()
        {

            DevExpress.XtraGrid.Columns.GridColumn colMa = new DevExpress.XtraGrid.Columns.GridColumn();
            colMa.Caption = "Mã";
            colMa.FieldName = "Ma";
            colMa.Name = "colMa";
            colMa.Visible = true;
            colMa.VisibleIndex = 0;

            DevExpress.XtraGrid.Columns.GridColumn colTen = new DevExpress.XtraGrid.Columns.GridColumn();
            colTen.Caption = "Tên";
            colTen.FieldName = "Ten";
            colTen.Name = "colTen";
            colTen.Visible = true;
            colTen.VisibleIndex = 1;

            DevExpress.XtraGrid.Columns.GridColumn colTaiKhoanThanhToanMacDinhID = new DevExpress.XtraGrid.Columns.GridColumn();
            colTaiKhoanThanhToanMacDinhID.Caption = "Tài khoản thanh toán ID";
            colTaiKhoanThanhToanMacDinhID.FieldName = "Tài khoản thanh toán ID";
            colTaiKhoanThanhToanMacDinhID.Name = "colTaiKhoanThanhToanMacDinhID";
            colTaiKhoanThanhToanMacDinhID.Visible = true;
            colTaiKhoanThanhToanMacDinhID.VisibleIndex = 2;

            this.gridViewDsDoiTuong.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] {
                colMa,colTen, colTaiKhoanThanhToanMacDinhID});
        }
        public override void LoadData()
        {
            try
            {
                gridDSDoiTuong.DataSource = DanhMucPhuongThucThanhToanBanHang.GetAll().DataView;
            }
            catch
            {
            }
        }
        public override void CreateNew()
        {
            try
            {
                FrmEditPhuongThucThanhToanBanHang frm = new FrmEditPhuongThucThanhToanBanHang(null);
                frm.ShowDialog();
            }
            catch
            {
            }
        }
        protected override void Edit()
        {
            try
            {

                int[] tmp = gridViewDsDoiTuong.GetSelectedRows();

                if (tmp != null && tmp.Length > 0)
                {
                    int selectedRowIndex = tmp[0];
                    object row = gridViewDsDoiTuong.GetRow(selectedRowIndex);
                    if (row != null && row is DataRowView)
                    {
                        Guid id = (Guid)((DataRowView)row)[0];
                        FrmEditPhuongThucThanhToanBanHang frm = new FrmEditPhuongThucThanhToanBanHang(DanhMucPhuongThucThanhToanBanHang.Instance.GetByID(id));
                        frm.ShowDialog();
                    }


                }
            }
            catch
            {
            }
        }
        protected override void Delete()
        {
            try
            {

                int[] tmp = gridViewDsDoiTuong.GetSelectedRows();

                if (tmp != null && tmp.Length > 0)
                {
                    int selectedRowIndex = tmp[0];
                    object row = gridViewDsDoiTuong.GetRow(selectedRowIndex);
                    if (row != null && row is DataRowView)
                    {
                        Guid id = (Guid)((DataRowView)row)[0];
                        PhuongThucThanhToanBanHang cv = DanhMucPhuongThucThanhToanBanHang.Instance.GetByID(id);
                        if (cv != null)
                        {
                            if (MessageBox.Show("Bạn có thực sự muốn xóa kho", "Xác nhận", MessageBoxButtons.YesNo) == DialogResult.No)
                                return;
                            else
                                cv.DeleteFromDB();
                        }
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}
